const sections = document.querySelectorAll('section[id]');

function scrollActive() {
  const scrollY = window.scrollY; // 卷去高度

  sections.forEach(current => {
    const sectionHeight = current.offsetHeight, // 自身高度
      sectionTop = current.offsetTop - 58,
      sectionId = current.getAttribute('id');
    if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
      document.querySelector('.nav-list a[href*=' + sectionId + ']').classList.add('active-link');
    } else {
      document
        .querySelector('.nav-list a[href*=' + sectionId + ']')
        .classList.remove('active-link');
    }
  });
}
window.addEventListener('scroll', scrollActive);
